package com.yjt.javatest.controller;

import com.yjt.javatest.entity.DTO.QuestionTestDTO;
import com.yjt.javatest.entity.Test;
import com.yjt.javatest.entity.DTO.UpdateQuestionTestDTO;
import com.yjt.javatest.entity.vo.TestInfo;
import com.yjt.javatest.service.TestService;
import com.yjt.javatest.utils.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/test")
@Tag(name = "测试")
public class TestController {
    @Resource
    TestService testService;


    @PostMapping("/createTest")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "创建测试",description = "废弃creator，增加userId")
    public APIResponse createTest(@RequestBody Test test) {
        return testService.createTest(test);
    }
    @PostMapping("/createTestFromTestLib/{testLibId}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "使用试卷模板创建测试")
    public APIResponse createTestByTestLib(@PathVariable Long testLibId, @RequestBody TestInfo testInfo) {
        return testService.createTestByTestLibId(testLibId, testInfo);
    }
    @GetMapping("/deleteTest/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "删除测试", description = "删除一份测试")
    public APIResponse deleteTest(@PathVariable Integer id) {
        return testService.deleteTest(id);
    }

    @PostMapping("/updateTest")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "修改测试信息", description = "修改测试信息")
    public APIResponse updateTest(@RequestBody Test test) {
        return testService.updateTest(test);
    }
    @GetMapping("/{id}")
    @PreAuthorize("@ex.isStudent()")
    @Operation(summary = "学生根据id生成并获取测试", description = "题目列表排列顺序根据老师的设置展示,没有答案")
    public APIResponse studentGetTestById(@PathVariable Long id) {
        return testService.studentGetTestById(id);
    }
    @GetMapping("/teacher/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据id获取测试",description = "题目顺序展示")
    public APIResponse teacherGetTestById(@PathVariable Long id) {
        return testService.teacherGetTestById(id);
    }

    @GetMapping("/teacher/preview/test/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据testId预览测试", description = "预览测试")
    public APIResponse teacherPreviewTest(@PathVariable Long id) {
        return testService.teacherPreviewTest(id);
    }

    @GetMapping("/teacher/preview/question/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据questionId预览测试", description = "预览题目")
    public APIResponse teacherPreviewQuestion(@PathVariable Long id) {
        return testService.teacherPreviewQuestion(id);
    }

    @PostMapping("/teacher/setStartTime/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据id修改测试开放时间")
    public APIResponse setStartTimeById(@PathVariable Long id, @RequestParam Date startTime) {
        return testService.setStartTime(id, startTime);
    }
    @PostMapping("/teacher/setEndTime/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据id修改测试结束时间")
    public APIResponse setEndTimeById(@PathVariable Long id, @RequestParam Date endTime) {
        return testService.setEndTime(id, endTime);
    }
    @PostMapping("/teacher/setAnswerReleaseTime/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师根据id修改测试答案公布时间")
    public APIResponse setAnswerReleaseTimeById(@PathVariable Long id, @RequestParam Date answerReleaseTime) {
        return testService.setAnswerReleaseTime(id, answerReleaseTime);
    }
    @PostMapping("/teacher/setOpenStatusOn/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师打开测试开放状态")
    public APIResponse setOpenStatusOn(@PathVariable Long id) {
        return testService.setOpenStatusOn(id);
    }

    @PostMapping("/teacher/finishTest/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师结束测试")
    public APIResponse finishTest(@PathVariable Long id) {
        return testService.finishTest(id);
    }

    @PostMapping("/teacher/setAnswerReleaseStatusOn/{id}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "教师打开测试公布状态")
    public APIResponse setAnswerReleaseStatusOn(@PathVariable Long id) {
        return testService.setAnswerReleaseStatus(id);
    }
    @PostMapping("/order")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "修改测试中题目顺序")
    public APIResponse changeQuestionOrder(@RequestBody UpdateQuestionTestDTO updateQuestionTestDTO) {
        return testService.changeQuestionOrder(updateQuestionTestDTO);
    }
    @GetMapping("/released")
    @Operation(summary = "获取已发布的测试", description = "传入教师id")
    public APIResponse getReleasedTests(@RequestParam("teacherId") Integer teacherId) {
        return testService.getReleasedTests(teacherId);
    }

    @GetMapping
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "获取当前用户所有测试")
    public APIResponse getTests() {
        return testService.getTests();
    }

    @PostMapping("/addQuestionToTest")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "添加问题到测试",description = "仅能把自己的问题添加到自己的测试")
    public APIResponse addQuestionToTest(@RequestParam Integer questionId,@RequestParam Integer testId, @RequestParam Integer score) {
        return testService.addQuestionToTest(questionId,testId, score);
    }

    @PostMapping("/addQuestionsToTest")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "批量添加问题到测试",description = "仅能把自己的问题添加到自己的测试")
    public APIResponse addQuestionsToTest(@RequestParam Long testId, @RequestBody List<QuestionTestDTO> questionInfo) {
        return testService.addQuestionsToTest(testId, questionInfo);
    }

    @PostMapping("/deleteQuestionFromTest/{questionId}/{testId}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "删除测试中的问题",description = "仅能自己删除自己的问题")
    public APIResponse deleteQuestionFromTest(@PathVariable Integer questionId,@PathVariable Integer testId) {
        return testService.deleteQuestionFromTest(questionId,testId);
    }
    @GetMapping("/studentInfo/export/{opt}")
    @PreAuthorize("@ex.isTeacher()")
    @Operation(summary = "导出某几次考试所有学生信息", description = "opt = 0 缺考记为0且记录进平均分， 1缺考记为0且不记录进平均分，2缺考记为空切记录进平均分")
    public APIResponse exportStudentInfoByTestIds(@RequestBody List<Integer> testIds, @PathVariable Integer opt, HttpServletResponse response) {
        return testService.exportStudentInfoByTestIds(testIds, opt, response);
    }

}
